//
//  SearchViewController.m
//  EasyToo
//
//  Created by zhengzhe on 13-9-14.
//
//

#import "SearchViewController.h"
#import "Goods.h"
#import "UIImageView+OnlineImage.h"
#import "GoodsDetailViewController.h"
#import "NetworkAlertView.h"
@interface SearchViewController ()

@end

@implementation SearchViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.navigationItem setTitle:@"搜索"];

    [self.view setBackgroundColor:BackgroundColor];
    
    [self createSearchBar];
    [self createTableView];
    
    _searchType = SEARCH_TYPE_GOODS;
    [[SearchManager shareManager] setDelegate:self];
	// Do any additional setup after loading the view.
    
    [[NetworkAlertView shareView] showNetView:@"搜索中..."];
    
    //随便搜点
    [[SearchManager shareManager] searchGoods:@"1"];
        
}

- (void)dealloc
{
    [[SearchManager shareManager] setDelegate:nil];
    RelaseAndNil(_searchTF);
    RelaseAndNil(_contentTable);
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)createSearchBar
{
    float adjustHeight = IOS7_OR_MORE?44:0;
    _searchTF = [[UITextField alloc] initWithFrame:CGRectMake(10, adjustHeight + 10, [DeviceUtil getScreenWidth] - 80, 40)];
    _searchTF.font = [UIFont boldSystemFontOfSize:26];
    _searchTF.borderStyle = UITextBorderStyleRoundedRect;
    _searchTF.layer.borderColor = [UIColor grayColor].CGColor;
    _searchTF.layer.borderWidth = 1;
    _searchTF.layer.cornerRadius = 10;
    _searchTF.delegate = self;
    [_searchTF setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:_searchTF];

    UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    searchBtn.frame = CGRectMake(CGRectGetMaxX(_searchTF.frame) + 5, CGRectGetMinY(_searchTF.frame), 55, 40);
    [searchBtn setBackgroundImage:[UIImage imageNamed:@"next1"] forState:UIControlStateNormal];
    [searchBtn setTitle:@"搜索" forState:UIControlStateNormal];
    [searchBtn addTarget:self action:@selector(searchEvent) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:searchBtn];
}


- (void)createTableView
{
    _contentTable = [[UITableView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_searchTF.frame) + 10, [DeviceUtil getScreenWidth], [DeviceUtil getScreenHeight] - 160)];
    _contentTable.dataSource = self;
    _contentTable.delegate = self;
    
    [self.view addSubview:_contentTable];
}

- (NSMutableArray*)currentResultArray
{
    if (_searchType == SEARCH_TYPE_GOODS)
    {
        return self.goodsArray;
    }
    else if (_searchType == SEARCH_TYPE_COUPON)
    {
        return self.couponArray;
    }
    
    return nil;
}

- (void)searchEvent
{
    [_searchTF resignFirstResponder];
    
    [[NetworkAlertView shareView] showNetView:@"搜索中..."];
    
    if (_searchType == SEARCH_TYPE_GOODS)
    {
        [[SearchManager shareManager] searchGoods:_searchTF.text];
    }
    else if (_searchType == SEARCH_TYPE_COUPON)
    {
        [[SearchManager shareManager] searchCoupon:_searchTF.text];
    }
    
}

#pragma mark tableview datasource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[self currentResultArray] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_searchType == SEARCH_TYPE_GOODS) {
        return [self getGoodsCellView:indexPath];
    }
    else
    {
        return nil;
    }
}

- (UITableViewCell*)getGoodsCellView:(NSIndexPath*)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    
    //加载图像
    UIImageView *imgview  = [[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"0.jpg"]]autorelease];
    imgview.frame = CGRectMake(10, 10, 100, 100);
    imgview.backgroundColor = [UIColor whiteColor];
    [cell addSubview:imgview];
    
    //加载名字
    UILabel *namelbl = [[[UILabel alloc]initWithFrame:CGRectMake(120, 15, 160, 50)]autorelease];
    namelbl.font = [UIFont boldSystemFontOfSize:15];
    namelbl.textColor = [UIColor darkGrayColor];
    namelbl.lineBreakMode = UILineBreakModeTailTruncation;
    namelbl.numberOfLines = 0;
    namelbl.adjustsFontSizeToFitWidth = YES;
    [cell addSubview:namelbl];
    
    //加载描述
    UILabel *desclbl = [[[UILabel alloc]initWithFrame:CGRectMake(120, 75, 160, 20)]autorelease];
    desclbl.font = [UIFont boldSystemFontOfSize:18];
    desclbl.textColor = [UIColor redColor];
    desclbl.lineBreakMode = UILineBreakModeWordWrap;
    desclbl.numberOfLines = 0;
    [cell addSubview:desclbl];
    
    //    }
    Goods *goodsInfo = [self.goodsArray objectAtIndex:indexPath.row];
    
    namelbl.text = goodsInfo.name;
    desclbl.text = [NSString stringWithFormat:@"￥%.2f", goodsInfo.salePrice];
    [imgview setOnlineImage:goodsInfo.imgUrl];
    
    return cell;

}

#pragma mark tableview delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_searchType == SEARCH_TYPE_GOODS) {
        return 120;
    }
    return 120;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Goods *goodsInfo = [self.goodsArray objectAtIndex:indexPath.row];
    
    GoodsDetailViewController *goodsDetailCtrl = [[GoodsDetailViewController alloc] init];
    goodsDetailCtrl.goodsInfo = goodsInfo;
    [self.navigationController pushViewController:goodsDetailCtrl animated:YES];
    [goodsDetailCtrl release];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

#pragma mark SearchManager delegate

- (void)onSearchGoodsFinish:(BOOL)isSuccess
                 GoodsArray:(NSMutableArray*)goods
{
    [[NetworkAlertView shareView] delView];
    if (isSuccess) {
        self.goodsArray = goods;
        [_contentTable reloadData];
    }
    else
    {
        self.goodsArray = nil;
        [_contentTable reloadData];
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"未搜索到相关产品" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
    }
    
}

- (void)onSearchCouponFinish:(BOOL)isSuccess
                 couponArray:(NSMutableArray*)couponArray
{
}


@end
